﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Text;

namespace nudota
{
    class PerformanceRender
    {
        private int m_ticks, m_max;
        private Queue<int> m_data=new Queue<int>();
        private Pen m_pen = new Pen(Color.LawnGreen, 1);
        private Pen m_grid_pen = new Pen(Color.Green, 1);

        private void init(int ticks)
        {
            m_max = 0;
            m_ticks=ticks;
            m_data.Clear();
            for (int i=0; i<ticks; ++i)
                m_data.Enqueue(0);
        }

        public PerformanceRender()
        {
            init(20);
        }

        public PerformanceRender(int ticks)
        {
            init(ticks);
        }

        public void put(int n)
        {
            while (m_data.Count >= m_ticks) m_data.Dequeue();
            m_data.Enqueue(n);

            m_max = m_data.Peek();
            foreach (int y in m_data)
                if (y > m_max) m_max = y;
        }

        private void draw_grid(Graphics g, int width, int height)
        {
            for (int x = 0; x < width; x += 10)
            {
                g.DrawLine(m_grid_pen, x, 0, x, height);
            }
            for (int y = height; y > 0; y -= 10)
            {
                g.DrawLine(m_grid_pen, 0, y, width, y);
            }
        }

        private PointF[] calc_points(int width, int height)
        {
            float xscale = (float)width / (m_ticks - 1);
            float yscale = (float)height / (m_max + 1) / 1.1f;

            int i = 0;
            PointF[] points = new PointF[m_data.Count];
            foreach (int y in m_data)
            {
                points[i].X = i * xscale;
                points[i].Y = height - y * yscale;
                i++;
            }

            return points;
        }

        public void draw(Graphics g, int width, int height)
        {
            g.Clear(Color.Black);
            draw_grid(g, width, height);
            g.DrawCurve(m_pen, calc_points(width, height));
        }

        public void draw(Control c)
        {
            Graphics g=c.CreateGraphics();
            draw(g, c.Width, c.Height);
            g.Dispose();
        }
    }
}
